//
//  PolSubDataHandler.js
//  SugLisSystem
//
//  Created by Mr.Right on 2013-05-09.
//  Copyright 2013 Mr.Right. All rights reserved.
//
function PolSubDataHandler(InvestRiskDataSet, period, appage, standByFlag3) {
	this.InvestRiskDataSet = InvestRiskDataSet;
	this.period = period;
	this.appage = appage;
	this.standByFlag3 = standByFlag3;
}

PolSubDataHandler.prototype.calculate = function(tableType, tablePattern, dataType) {
	if (1 == tableType) {
		if (1 == tablePattern) {
			return getPolSubData1(dataType, this.standByFlag3, this.period, this.InvestRiskDataSet, this.appage);
		} else if (2 == tablePattern) {
			return getPolSubData2(dataType, this.standByFlag3, this.period, this.InvestRiskDataSet, this.appage);
		} else if (3 == tablePattern) {
			return getPolSubData3(dataType, this.standByFlag3, this.period - 1, this.InvestRiskDataSet, this.appage);
		} else if (4 == tablePattern) {
			return getPolSubData4(dataType, this.standByFlag3, this.period, this.InvestRiskDataSet, this.appage);
		}
	} else if (2 == tableType) {
		if (1 == tablePattern) {
			return getPolSubDatas1(dataType, this.standByFlag3, this.period, this.InvestRiskDataSet, this.appage);
		} else if (2 == tablePattern) {
			return getPolSubDatas2(dataType, this.standByFlag3, this.period, this.InvestRiskDataSet, this.appage);
		} else if (3 == tablePattern) {
			return getPolSubDatas3(dataType, this.standByFlag3, this.period, this.InvestRiskDataSet, this.appage);
		} else if (4 == tablePattern) {
			return getPolSubDatas4(dataType, this.standByFlag3, this.period, this.InvestRiskDataSet, this.appage);
		}
	}

	return [];
}
function getPolSubData3(type, signNo, peroid, inInvestRiskDataSet, appage) {
	var tempInvestRiskDataSet = [];
	var size = inInvestRiskDataSet.length;
	if (signNo != null && signNo != '' && size > 0) {
		for (var i = 0; i < size; i++) {
			var tempInvestRiskDataSchema = inInvestRiskDataSet[i];
			if (type == tempInvestRiskDataSchema['type'] && signNo == tempInvestRiskDataSchema['signno']) {
				tempInvestRiskDataSet.push(tempInvestRiskDataSchema);
			}
		}
	}

	var result = [];
	for (var i = 0; i < peroid + 1; i++) {
		result.push(0);
	}

	for (var index = 0; index < peroid; index++) {
		for (var n = 0; n < tempInvestRiskDataSet.length; n++) {
			var tempInvestRiskDataSchema = tempInvestRiskDataSet[n];
			var age = parseInt(tempInvestRiskDataSchema['age']);
			var degree = parseInt(tempInvestRiskDataSchema['degree']);
			var rs = parseFloat(tempInvestRiskDataSchema['value']);
			if (age > index && age + index <= index) {
				result[index] += rs;
			}
		}
	}
	return result;
}

module.exports = PolSubDataHandler;
